import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text("InheritedWidget数据共享"),
      ),
      body: HomeScreen(),
    ),
  ));
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int count = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          child: ShareDataWidget(
            clickCount: count,
            child: Column(
              children: <Widget>[
                Column(
                  children: <Widget>[
                    TesetOhterWidget(),
                    TesetOhterWidget(),
                    TesetOhterWidget()
                  ],
                ),
                RaisedButton(
                    child: Text("$count"),
                    onPressed: () {
                      setState(() {
                        count++;
                      });
                    })
              ],
            ),
          ),
        ),
      ],
    );
  }
}

class ShareDataWidget extends InheritedWidget {
  int clickCount = 0;



  ShareDataWidget({this.clickCount, Widget child}) : super(child: child);

  static ShareDataWidget of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(ShareDataWidget);
  }

  void updateCount(int count){
  }

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    if (oldWidget is ShareDataWidget) {
      return oldWidget.clickCount != clickCount;
    }
    return false;
  }
}

class TesetOhterWidget extends StatefulWidget {
  @override
  _TesetOhterWidgetState createState() => _TesetOhterWidgetState();
}

class _TesetOhterWidgetState extends State<TesetOhterWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(
          child: Text(ShareDataWidget.of(context).clickCount.toString()),//
          onPressed: () {
            setState(() {
              ShareDataWidget.of(context).clickCount++;
            });
          }),
    );
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("didChangeDependencies");
  }
}
